package mysort;

public class a03_InsertDemo1 {
    public static void main(String[] args) {
        //插入排序
        //将0~N索引当做有序的，从N+1索引到最后一个当成是无序的
        //遍历无序的数组，插入有序数组中的适当位置

        int[] arr = {3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48};

        //确定无序的数据从那个索引开始
        int startIndex = -1;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i]>arr[i+1]){
                startIndex = i+1;
                System.out.println(startIndex);
                break;
            }
        }

        //从无序的数据开始比较
        for (int i = startIndex; i < arr.length; i++) {
            int j = i;
            while(j>0 && arr[j]<arr[j-1]){
                //交换位置
                int temp = 0;
                temp = arr[j];
                arr[j] = arr[j-1];
                arr[j-1] = temp;
                j--;
            }



        }

        printArr(arr);




    }
    private static void printArr(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
        }


    }


}
